/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.utils.CacheUtils;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.expert.entity.Care;
import com.chlitina.store.modules.expert.entity.Product;
import com.chlitina.store.modules.expert.service.CareService;
import com.chlitina.store.modules.expert.utils.ExpertUtils;

/**
 * 系统护理Controller
 * @author liujiang
 * @version 2013-11-02
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/expert/care")
public class CareController extends BaseController {

	@Autowired
	private CareService careService;
	
	@ModelAttribute
	public Care get(@RequestParam(required=false) Long id) {
		if (id != null){
			return careService.get(id);
		}else{
			return new Care();
		}
	}
	
	private String getSelectPage(Page page, String careName){
		Long select_care_id = (Long) CacheUtils.get("select_care_id");
		String currentId = "";
		if(select_care_id != null) {
	        int index = careService.getIndexById(select_care_id, careName);
	        ExpertUtils.setPageNoByCurrentId(page, index);
	        currentId = String.valueOf(select_care_id);
	        CacheUtils.remove("select_care_id");
		}
		return currentId;
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Care care, HttpServletRequest request, HttpServletResponse response, Model model) {
		String careName = request.getParameter("careName");
		Page<Care> page = new Page<Care>(request, response);
		String currentId = getSelectPage(page,  careName);
		Page<Care> current_page = careService.find(page, careName, care); 
		model.addAttribute("careName", careName);
		model.addAttribute("currentId", currentId);
        model.addAttribute("page", current_page);
		return "modules/expert/careList";
	}

	@RequestMapping(value = "form")
	public String form(Care care, Model model) {
		CacheUtils.put("select_care_id", care.getId());
		model.addAttribute("care", care);
		return "modules/expert/careForm";
	}

	@RequestMapping(value = "save")
	public String save(Care care, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, care)){
			return form(care, model);
		}
		careService.save(care);
		addMessage(redirectAttributes, "保存系统护理'" + care.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/expert/care/list";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		careService.delete(id);
		addMessage(redirectAttributes, "删除系统护理成功");
		return "redirect:"+Global.ADMIN_PATH+"/expert/care/list";
	}
	
	@RequestMapping(value = {"detail", "desc"})
	public String desc(Care care, ModelMap modelMap) {
		modelMap.addAttribute("care", care);
		return "modules/expert/careDesc";
	}
}
